#pragma once

/**
- 阻塞队列
*/

#include <queue>
#include <memory>
#include <mutex>
#include <condition_variable>
#include "jgcxtools.h"

using namespace std;

template <class T>
class JGQueue
{
public:
	//typedef typename list<T>::iterator iterator;

	JGQueue() {};
	~JGQueue() {
		for (size_t i = 0; i < _queue.size(); i++)
		{
			_queue.pop();
		}
	};

	void add(T msgs) {
		unique_lock<mutex> lock(m_quotes_lock);
		_queue.push(msgs);
		_len++;
		m_action_cond.notify_all();
	};
	T get() {
		unique_lock<mutex> lock(m_quotes_lock);
		while (_queue.empty())
		{
			m_action_cond.wait(lock);
		}

		T tmp = _queue.front();
		_queue.pop();
		_len--;
		return tmp;
	};
	size_t size() { return _queue.size(); };
	size_t size_l() { return _len; };

	/*JGQueue::iterator& begin() { _it = _queue.begin(); return _it; };
	JGQueue::iterator& end() { _it = _queue.end(); return _it; };*/

private:
	size_t _len = 0;
	queue<T> _queue;
	mutex m_quotes_lock;
	condition_variable m_action_cond;
	//iterator _it;
};



